package com.wzqj.agent.app.user.service.impl;

import java.util.List;

import com.wzqj.agent.app.user.dao.AppSaleMerchantDao;
import com.wzqj.agent.app.user.dao.AppSalesDao;
import com.wzqj.agent.app.user.domain.SaleMerchant;
import com.wzqj.agent.app.user.domain.Sales;
import com.wzqj.agent.app.user.domain.UserInfo;
import com.wzqj.agent.app.user.service.ApiUserService;

import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wanghao@weipass.cn on 2015/7/31.
 */
@Service("apiUserService")
public class ApiUserServiceImpl implements ApiUserService {

    @Autowired
    private AppSalesDao salesDao;

    @Autowired
    private AppSaleMerchantDao saleMerchantDao;

    /**
    * 根据业务员手机号获取业务员信息
    * @param phone
    * @return
    */
    @Override
    public UserInfo getUserInfo(String phone) {

        Sales saler = salesDao.getByPhone(phone);
        if(saler == null){
            return null;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(saler.getId());
        userInfo.setName(saler.getName());
        userInfo.setEmployeeno(saler.getJobNum());
        userInfo.setPhone(saler.getPhone());
        userInfo.setIcon(saler.getHeadingUrl());
        userInfo.setJob(saler.getJob());
        userInfo.setStatus(1);
        return userInfo;
    }

    /**
     * 更新对应手机号业务员的头像信息
     * @param phone
     * @param iconUrl
     * @return
     */
    @Override
    public UserInfo updateUserInfo(String phone, String iconUrl) {

        Sales saler = new Sales();
        saler.setHeadingUrl(iconUrl);
        saler.setPhone(phone);
        int i = salesDao.updateIcon(saler);
        if(i == 1){//更新成功
            return getUserInfo(phone);
        }
        return null;
    }

    /**
     * 绑定门店到业务员
     * @param qualificationid
     * @param userId
     * @return
     */
    @Override
    public boolean bindMerchantToUser(String qualificationid, long userId) {

        boolean flag = false;
        SaleMerchant saleMerchant = new SaleMerchant();
        saleMerchant.setMerId(qualificationid);
        saleMerchant.setSaleId(userId);
        int insert = saleMerchantDao.insert(saleMerchant);
        if(insert > 0){
            flag = true;
        }
        return flag;
    }
    
    /**
     * 绑定商户到业务员（新）
     * @param qualificationid
     * @param userId
     * @return
     */
    @Override
    public boolean bindMerchantToUserNew(String qualificationId, long userId) {

        boolean flag = false;
        List<SaleMerchant> saleMerchants = saleMerchantDao.getByMerId(qualificationId);
        if (saleMerchants==null||saleMerchants.size()<=0) {
			
        	SaleMerchant saleMerchant = new SaleMerchant();
        	saleMerchant.setMerId(qualificationId);
        	saleMerchant.setSaleId(userId);
        	int insert = saleMerchantDao.insert(saleMerchant);
        	
        	if(insert > 0){
        		flag = true;
        	}
		} 
        return flag;
    }
}
